package com.action;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.form.LoginForm;

public class LoginAction extends Action {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		LoginForm loginForm = (LoginForm) form;

		String ID = loginForm.getRid();
		String Password = loginForm.getPwd();
		boolean flag = false;
		String actionpath = "";

		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;

		String url = "jdbc:mysql://localhost:3306/shangpin";
		String user = "root";
		String password = "rosy";

		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();

			conn = DriverManager.getConnection(url, user, password);

			String sql = "select * from userinfo where ID = '" + ID
					+ "' and Password='" + Password + "'";
			stmt = conn.prepareStatement(sql);
			// pstmt.setString(1, ID);
			// pstmt.setString(2, Password);
			rs = stmt.executeQuery(sql);

			if (rs.next()) {
				actionpath = "success";
			} else {
				actionpath = "fail";
			}

			// rs.close();
			// stmt.close();
		} catch (SQLException e) {
			// TODO 自動生成された catch ブロック
			System.out.println("Sql exception");
		} catch (ClassNotFoundException e) {
			System.out.println("Class not found");
		} catch (Exception e) {
			System.out.println("exception");
		} finally {
			try {
				if (conn != null || rs != null || stmt != null) {
					stmt.close();
					rs.close();
					conn.close();
				}
			} catch (SQLException e) {
				response.getWriter().println(e.getMessage());
			}
		}
		// return mapping.findForward("fail");

		// return mapping.findForward(actionpath);
		// return mapping.findForward(actionpath);
		return mapping.findForward(actionpath);
	}
}